package ml.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 读取数据库属性文件，获取数据库连接信息
 * 如何让用户只能创建一个ConfigManager?单例模式：（1）把构造方法私有（2）程序提供给别人唯一对象
 */
public class ConfigManager {
    private static ConfigManager configManager;
    //properties.load(InputStream);读取属性文件
    private static Properties properties;

    private ConfigManager(){
        String configFile= "ml/database.properties";
        properties=new Properties();
        InputStream in=ConfigManager.class.getClassLoader().getResourceAsStream(configFile);
        try {
            properties.load(in);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static ConfigManager getInstance(){
        if(configManager==null){
            configManager=new ConfigManager();
        }
        return configManager;
    }

    public String getString(String key){
        return properties.getProperty(key);
    }
}
